package com.atguigu.gmall.product.cdc;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.model.SkuInfo;
import com.atguigu.gmall.product.model.SkuInfoCDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 *
 */
@CanalTable("sku_info") //监控指定表变更操作
@Component
public class SkuInfoHandler implements EntryHandler<SkuInfoCDC> {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 监听到新增操作
     *
     * @param skuInfo
     */
    @Override
    public void insert(SkuInfoCDC skuInfo) {
        System.out.println("新增商品");
        System.out.println("skuInfo = " + skuInfo);
    }

    /**
     * 监听到修改操作
     *
     * @param before
     * @param after
     */
    @Override
    public void update(SkuInfoCDC before, SkuInfoCDC after) {
        //System.out.println("修改商品");
        //System.out.println("修改商品before：" + before);
        //System.out.println("修改商品after：" + after);
        try {
            //删除缓存
            String dataKey = RedisConst.SKUKEY_PREFIX + after.getId() + RedisConst.SKUKEY_SUFFIX;
            redisTemplate.delete(dataKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
            //todo 将删除缓存失败消息发送MQ-将来MQ消费者监听后进行重试
        }
    }

    /**
     * 监听到删除操作
     *
     * @param skuInfo
     */
    @Override
    public void delete(SkuInfoCDC skuInfo) {
        System.out.println("删除商品skuInfo = " + skuInfo);
    }


}
